﻿//-----------------------------------------------------------------------
// <copyright file="PrivateMessagesReadSettingsCommand.cs" company="AFS">
// Copyright (c) AFS. All rights reserved.
//
//    This file is part of Chatbox.
//
//    Chatbox is free software: you can redistribute it and/or modify
//    it under the terms of the GNU Lesser General Public License as published by
//    the Free Software Foundation, either version 3 of the License, or
//    any later version.
//
//    Chatbox is distributed in the hope that it will be useful,
//    but WITHOUT ANY WARRANTY; without even the implied warranty of
//    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
//    GNU Lesser General Public License for more details.
//
//    You should have received a copy of the GNU Lesser General Public License
//    along with Chatbox. If not, see http://www.gnu.org/copyleft/lesser.html
// </copyright>
//----------------------------------------------------------------------- 

namespace ChatBox.UI.Windows.PrivateMessages
{
    using ChatBox.DomainObjects.Settings;
    using ChatBox.Interfaces;
    using ChatBox.UI.Commands;
    using ChatBox.UI.MessageDisplay;
    using ChatBox.XamlUtility;

    /// <summary>
    /// Command to read settings into the PrivateMessagesViewModel
    /// </summary>
    public class PrivateMessagesReadSettingsCommand : GenericCommandBase<PrivateMessagesViewModel>
    {
        /// <summary>
        /// the source for Ui settings
        /// </summary>
        private readonly IUISettingsRepository uiSettingsRepository;

        /// <summary>
        /// Initializes a new instance of the PrivateMessagesReadSettingsCommand class
        /// </summary>
        /// <param name="uiSettingsRepository">the source of Ui settings</param>
        public PrivateMessagesReadSettingsCommand(IUISettingsRepository uiSettingsRepository)
        {
            this.uiSettingsRepository = uiSettingsRepository;
        }

        /// <summary>
        /// execute the command
        /// </summary>
        /// <param name="value">the viewmodel</param>
        public override void ExecuteOnValue(PrivateMessagesViewModel value)
        {
            value.PrivateMessageFormatOptions = this.GetPrivateMessageFormatOptions(value);

            value.LoadWindowPosition();
            value.Filters.ReadSettingsCommand.Execute(value.Filters);
        }

        /// <summary>
        /// Getnerate Fomat options ofr private messages
        /// </summary>
        /// <param name="viewModel">the view model</param>
        /// <returns>the format options</returns>
        private FormatOptions GetPrivateMessageFormatOptions(PrivateMessagesViewModel viewModel)
        {
            UISettingsData uiSettings = this.uiSettingsRepository.ReadSettings();

            FormatOptions formatOptions = new FormatOptions(uiSettings.PrivateMessageFontSize, DateTimeDisplay.DateTime, false);
            formatOptions.ShowSendPrivateMessageButton = uiSettings.ShowSendPrivateMessageButtonInPrivateMessages;
            formatOptions.ShowDeleteMessageButton = uiSettings.ShowDeleteMessageButtonInPrivateMessages;

            formatOptions.SendPrivateMessageButtonStyle = viewModel.SendPrivateMessageButtonStyle;
            formatOptions.DeleteMessageButtonStyle = viewModel.DeleteMessageButtonStyle;

            formatOptions.SendPrivateMessageButtonHandler = viewModel.SendPrivateMessageClick;
            formatOptions.DeleteMessageButtonHandler = viewModel.DeleteMessageClick;

            formatOptions.ShowSendPrivateMessageButton = uiSettings.ShowSendPrivateMessageButtonInPrivateMessages;
            formatOptions.ShowDeleteMessageButton = uiSettings.ShowDeleteMessageButtonInPrivateMessages;

            return formatOptions;
        }
    }
}
